#include "Draw.h"


 int   Draw::SCREEN_HEIGHT = 0;
int	Draw::SCREEN_WIDTH = 0;
Draw::Draw(void)
{
}


Draw::~Draw(void)
{
}

void Draw::init()
{
	SDL_Rect** modes;
    int i;
    /* Get available fullscreen/hardware modes */
    modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
   
    /* Check if there are any modes available */
    if (modes == (SDL_Rect**)0) {
        printf("No modes available!\n");
       exit(-1);
   }
   
   /* Check if our resolution is restricted */
   if (modes == (SDL_Rect**)-1) {
       printf("All resolutions available.\n");
   }
   else{
       /* Print valid modes */
       printf("Available Modes\n");
	   int largest=0;
      for (i=0; modes[i]; ++i)
	  {
        printf("  %d x %d\n", modes[i]->w, modes[i]->h);
		if(modes[i]->w>modes[largest]->w)
		{
			largest = i;
		}
	  }
	  SCREEN_HEIGHT = modes[largest]->h;
	  SCREEN_WIDTH = modes[largest]->w;
	  
   }
}

SDL_Surface* Draw::load_image(std::string const& path, bool alpha) {
	::SDL_Surface *result = ::IMG_Load(path.c_str());

	if (result) {

		::SDL_Surface *tmp;
		if (alpha) {
			tmp = ::SDL_DisplayFormatAlpha(result);

		}
		else {
			tmp = ::SDL_DisplayFormat(result);

		}
		if (tmp) {
			::SDL_FreeSurface(result);
			result = tmp;

		}
		else {
			// signal the error, return the original, unconverted
			// surface

		}
	}
	else {

	}
	return result;
}
void Draw::apply_surface(const int x, const int y,  SDL_Surface* source, SDL_Surface* destination,  SDL_Rect* clip  )//clip is the clip taken from the SOURCE. it has x,y,w, and h.
{
	//Holds offsets
	SDL_Rect offset;

	//Get offsets
	offset.x = x;
	offset.y = y;

	//Blit
	SDL_BlitSurface( source, clip, destination, &offset );
}

double Draw::distanceFormula(int x1, int y1, int x2, int y2)
{
	//sqrt( (float) (x1-x2)^2 + (y1-y2)^2)
	 return 
	 std::sqrt( (double)((x1-x2)^2 + (y1-y2)^2));
 
}

